package com.good.webadmin.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.good.domain.Utils.MyStringUtils;
import com.good.domain.controller.BaseController;
import com.good.domain.entity.AdminModule;
import com.good.domain.models.PostID;
import com.good.domain.models.Result;
import com.good.domain.service.IAdminLogService;
import com.good.domain.service.IAdminModuleService;
import com.good.domain.service.IUserLoginToken;
import com.good.webadmin.Models.Params.AddModule;
import com.good.webadmin.Models.Params.EditModule;
import com.good.webadmin.Models.TokenUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lqz
 * @since 2022-03-05
 */
@RestController
@RequestMapping("/webadmin/admin-module")
@Api(tags = "菜单管理模块")
public class AdminModuleController extends BaseController {
    @Autowired
    private IAdminModuleService iAdminModuleService;
    @Autowired
    private IAdminLogService adminLogService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加功能模块")
    public Result Add(TokenUser tokenUser, @RequestBody AddModule addModule) {
        if (MyStringUtils.isBlank(addModule.getPath())) {
            return OtherError("模块名称和路径不能为空");
        }
        QueryWrapper<AdminModule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", addModule.getName());
        queryWrapper.eq("path", addModule.getPath());
        AdminModule adminModule = iAdminModuleService.getOne(queryWrapper);
        if (adminModule == null) {
            adminModule = new AdminModule();
            adminModule.setName(addModule.getName());
            adminModule.setDescription(addModule.getDescription());
            adminModule.setUid(tokenUser.getUserId());
            adminModule.setPath(addModule.getPath());
            adminModule.setParentId(addModule.getParentId());
            adminModule.setSn(addModule.getSn());
            adminModule.setIcon(addModule.getIcon());
            iAdminModuleService.save(adminModule);
            //日志输出
            adminLogService.setAdminLog(tokenUser.getUserId(), "[菜单模块添加成功]", addModule);
            return Success("添加模块成功");
        } else {
            return OtherError("模块名称和路径不能相同");
        }
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ApiOperation("删除模块")
    @IUserLoginToken
    public Result Del(TokenUser tokenUser, @RequestBody PostID postID) {

            var ret = iAdminModuleService.removeById(postID.getId());
            if (ret) {
                //日志输出
                adminLogService.setAdminLog(tokenUser.getUserId(), "[菜单模块删除成功]", postID);
                return Success("删除模块成功");
            } else {
                return OtherError("删除模块失败");
            }

    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation("修改模块")
    public Result Edit(TokenUser tokenUser, @RequestBody EditModule editModule) {
        if (MyStringUtils.isBlank(editModule.getPath()) ) {
            return OtherError("模块名称和路径不能为空");
        }
        QueryWrapper<AdminModule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", editModule.getName());
        queryWrapper.eq("path", editModule.getPath()).ne("id", editModule.getId());
        AdminModule adminModule = iAdminModuleService.getOne(queryWrapper);
        if (adminModule == null) {
        } else {
            if (adminModule.getId() != editModule.getId()) {
                return OtherError("模块名称和路径不能相同");
            }
        }
        adminModule = iAdminModuleService.getById(editModule.getId());
        adminModule.setName(editModule.getName());
        adminModule.setDescription(editModule.getDescription());
        adminModule.setUid(tokenUser.getUserId());
        adminModule.setPath(editModule.getPath());
        adminModule.setParentId(editModule.getParentId());
        adminModule.setSn(editModule.getSn());
        adminModule.setIcon(editModule.getIcon());
        var ret = iAdminModuleService.updateById(adminModule);
        if (ret) {
            //日志输出
            adminLogService.setAdminLog(tokenUser.getUserId(), "[菜单模块修改成功]", editModule);
            return Success("模块成功");
        } else {
            return Error500("修改模块失败");
        }
    }

    @PostMapping("ListModule")
    @ApiOperation("模块列表")
    @IUserLoginToken
    public Result ListModule() {
        QueryWrapper<AdminModule> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("sn");
        var modules = iAdminModuleService.listMaps(queryWrapper);
        return Success(modules);
    }
}
